package com.imdroid.carac.activities;

import android.content.res.Resources;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.util.Log;

import com.imdroid.carac.IMDroid;
import com.imdroid.carac.R;
import com.imdroid.carac.process.ProcessFactory;
import com.imdroid.carac.process.UserProcess;

/**
 * The activities to adjust the parameters
 * 
 */
@SuppressWarnings("deprecation")
public class OptionsActivity extends PreferenceActivity {
	/**
	 * The tag of the options
	 */
	private static String TAG = "OptionsActivity";

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
	 */

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.options);
		setPreferenceConstraints();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.preference.PreferenceActivity#onStop()
	 */
	public void onStop() {
		// stop 前保存数据到服务器
		Log.d(TAG, "onStop");

		// 更新用户资料到服务器
		new Thread() {
			public void run() {
				IMDroid.refreshUser();
				UserProcess process = (UserProcess) ProcessFactory
						.getProcess(UserProcess.class);
				process.postUser(IMDroid.getUser());
			}
		}.start();

		super.onStop();
	}

	/**
	 * Set the preference constraints.
	 */
	private void setPreferenceConstraints() {
		Resources res = getResources();

		// 初始化事件
		EditTextPreference userName = (EditTextPreference) findPreference(res
				.getString(R.string.KEY_USER_NAME));
		EditTextPreference mobilePhone = (EditTextPreference) findPreference(res
				.getString(R.string.KEY_MOBILE_PHONE));
		EditTextPreference vehicleModel = (EditTextPreference) findPreference(res
				.getString(R.string.KEY_VEHICLE_MODEL));
		EditTextPreference boardPlate = (EditTextPreference) findPreference(res
				.getString(R.string.KEY_BOARD_PLATE));
		EditTextPreference frameNumber = (EditTextPreference) findPreference(res
				.getString(R.string.KEY_FRAME_NUMBER));
		EditTextPreference engineNumber = (EditTextPreference) findPreference(res
				.getString(R.string.KEY_ENGINE_NUMBER));
		EditTextPreference nickName = (EditTextPreference) findPreference(res
				.getString(R.string.KEY_NICKNAME));
//		Preference weiboAccount = findPreference(res
//				.getString(R.string.KEY_WEIBO_ACCOUNT));

		// 字段长度限制
		userName.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(60) });
		mobilePhone.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(32),
						new DigitsKeyListener() {
							protected char[] getAcceptedChars() {
								return new char[] { '1', '2', '3', '4', '5',
										'6', '7', '8', '9', '0', '+', '*', '#' };
							}
						} });
		mobilePhone.getEditText().setInputType(InputType.TYPE_CLASS_PHONE);
		vehicleModel.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(12) });
		boardPlate.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(12) });
		frameNumber.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(18) });
		engineNumber.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(18) });
		nickName.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(48) });

		OptionsSeekBarPreference rec_file_len = (OptionsSeekBarPreference) findPreference(res
				.getString(R.string.KEY_REC_FILE_LEN));
		rec_file_len.setRange(10, 1200, "秒");

		OptionsSeekBarPreference rec_file_count = (OptionsSeekBarPreference) findPreference(res
				.getString(R.string.KEY_REC_FILE_COUNT));
		rec_file_count.setRange(50, 360, "个");

		OptionsSeekBarPreference sec_add_cen = (OptionsSeekBarPreference) findPreference(res
				.getString(R.string.KEY_SEC_ADD_CEN));
		sec_add_cen.setRange(1, 150, null);

		// 微博用户事件
//		weiboAccount
//				.setOnPreferenceClickListener(new OnPreferenceClickListener() {
//					public boolean onPreferenceClick(Preference preference) {
//						Weibo.forceAuth(OptionsActivity.this);
//						return true;
//					}
//				});

		findPreference(res.getString(R.string.KEY_REC_BACKGOUND))
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {
					public boolean onPreferenceClick(Preference preference) {
						if (IMDroid.getRecordBackground()) {
							FloatView.show();
							// Record.start();
						} else {
							Record.stop();
							FloatView.hide();
						}
						return false;
					}
				});
	}

}
